<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

// 上传章节到upyun
$GLOBALS['config']['master']['db_database'] = 'gouyg';
$GLOBALS['config']['slave']['db_database']  = 'gouyg';

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(2);

$basedir = dirname(__FILE__);
$model = new Db;
$limit = 1000;
$start_id = $redis->get('Remote_start_id');
if(!$start_id) {
    $start_id = 0;
}

while ($limit--) {
    echo2('limit:'.$limit.', start_id:'.$start_id);
    $info = $model->find('book_chapter', "remote_file=1 and id>{$start_id}", "id asc");
    $filename = $basedir . $info['local_file'];
    if($info && file_exists($filename)) {   
        unlink($filename);
        $redis->lPush('BOOK_LOCAL_REMOVE2', date('Y-m-d H:i:s').' '.$info['local_file']);
  
        echo2($info['id'].' local removed, '.$info['local_file']);
        $start_id = $info['id'];
    }
    elseif(!$info) {
        echo2('No Valid File');
        sleep(2);
    }
    elseif($info) {
        $start_id = $info['id'];

        echo2($info['id'].' local had removed, '.$info['local_file']);
    }
}

$redis->set('Remote_start_id', $start_id);
sleep(2);

